<?php

include dirname(dirname(dirname(__FILE__))) . '/include/common.php';

class webServiceUpdateBudget extends core {

    public function process() {
        $data = array();
        $item_id = intval($_POST['item_id']);
        $user_id = intval($_POST['user_id']);
        if($_POST['item']===""){
             $this->_JSONResponse(array('error' => 1, 'msg' => "Please enter Item name."));
        }
        $amount = preg_replace('/[^0-9\.]/', '', $_POST['amount']);
        $amount =floatval($amount);
         if($amount <=0){
             $this->_JSONResponse(array('error' => 1, 'msg' => "Please enter your budget for this item."));
        }
        $result_spent = $this->db->get_row("SELECT SUM(item_amount) AS total_spent, budget_goal FROM budget_items,users WHERE user_id=$user_id AND budget_items.user_id = users.id");
        
        if($result_spent){
            $money_spent = $result_spent->total_spent;
            $goal = $result_spent->budget_goal;
  
//            if($goal < $money_spent + $amount){
//                $this->_JSONResponse(array('error' => 1, 'msg' => "Budget goal cannot be less than Budget spent, please increase Budget goal."));
//               
//            }
        }else{
            $this->_JSONResponse(array('error' => 1, 'msg' => "Oops, something went wrong. Please try again."));
        }
        
        $data['user_id'] = $user_id;
        $data['item_name'] = strip_tags(trim($_POST['item']));
        $data['item_amount'] = $amount;
        $fields = array();
        $values = array();
        $this->_preDataForInsert($data, $fields, $values);
        $fields = implode(',', $fields);
        $values = implode(',', $values);
        
        if ($item_id > 0) {
            $result_budget = $this->db->get_row("SELECT * FROM budget_items WHERE item_id=$item_id");
           
            if ($result_budget) {
               
                $item_name = $this->db->escape($data['item_name']); 
                $item_amount= $data['item_amount'];
                $user_id = $data['user_id'];
          
                $query = "UPDATE `budget_items` SET `item_name`='".$item_name."',`item_amount`='$item_amount',
                        `user_id`='$user_id' WHERE `item_id`= '$item_id'";
               
                if ($this->db->query($query)===false) {

                    $this->_JSONResponse(array('error' => 1, 'msg' => 'Oops, something went wrong. Please try again'));
                } else {
                    $this->_JSONResponse(array('error' => 0, 'msg' => 'Changes have been saved'));
                }
            } else {
                $this->_JSONResponse(array('error' => 1, 'msg' => 'Item does not exist'));
            }
        } else {
            if ($this->db->query("INSERT INTO `budget_items` ({$fields}) VALUES ({$values});")===false) {

                $this->_JSONResponse(array('error' => 1, 'msg' => 'Oops, something went wrong. Please try again'));
            } else {
                $this->_JSONResponse(array('error' => 0, 'msg' => 'Item has been added'));
            }
        }
    }

}

$processor = new webServiceUpdateBudget();
$processor->process();
?>